import { mapGetters } from 'vuex';
import moment from 'moment';
import common from '../common.js';
import { formatDate, formatTen } from '../../assets/js/help.js';
import { cloneDeep } from 'lodash';
export default {
  mixins: [common],
  props: {
    areaTreeData: Array,
    channelTreeData: Array,
    searchList: Array,
    singleWidth: {
      type: [String, Number],
      default: '140'
    },
    tableBoxId: {
      type: String,
      default: ''
    },
    isSetSearch: {
      type: [String, Boolean],
      default: true
    },
    isSetSingle: {
      type: Boolean,
      default: true
    },
    isCanSwitchStatus: {
      type: Boolean,
      default: true
    },
    isNeedTree: {
      type: Boolean,
      default: false
    },
    paramName: String,
    isCheckbox: Boolean,
    isNeedAreaTree: {
      type: Boolean,
      default: false
    },
    paramNameForAreaTree: String,
    isNeedChannelTree: {
      type: Boolean,
      default: false
    },
    paramNameForChannelTree: String,
    reloadChannelTree: {
      type: Boolean,
      default: false
    },
    reloadAreaTree: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isSingle: this.isSetSingle, //是单项还是多项选择条件，true为单项，false为多项
      singleIndex: 0,
      isChangeH: false,
      selectionList: this.searchList
    };
  },
  computed: {
    ...mapGetters({
      isFullScreenTable: 'getFullScreenTable',
      isMask: 'getIsMask'
    })
  },
  created() {
    let params = {};
    if (this.isNeedTree) {
      params['isNeedTree'] = true;
    }
    let userLoginId = window.sessionStorage.getItem('loginid');
    let curPagePath = userLoginId + '-' + this.$route.path;
    let curPageCache = window.localStorage.getItem(curPagePath);
    let _curPageCache = curPageCache ? JSON.parse(curPageCache) : {};
    if (_curPageCache.parkIds) {
      params['parkIds'] = _curPageCache.parkIds;
      params['parkIdParamName'] = this.paramName;
      params['parkSysType'] = _curPageCache.parkSysType;
    }
    if (_curPageCache.areaIds) {
      params['areaIds'] = _curPageCache.areaIds;
    }
    if (_curPageCache.enterChannelNames) {
      let paramVal = _curPageCache.enterChannelNames.split(',');
      let paramName = paramVal.length > 0 ? 'enterChannelNames' : 'enterChannelName';
      params['enterChannelParamName'] = paramName;
      params['enterChannelNames'] = paramVal.join(',');
    }
    if (_curPageCache.leaveChannelNames) {
      let paramVal = _curPageCache.leaveChannelNames.split(',');
      let paramName = paramVal.length > 0 ? 'leaveChannelNames' : 'leaveChannelName';
      params['leaveChannelParamName'] = paramName;
      params['leaveChannelNames'] = paramVal.join(',');
    }
    this.confirmSearch(params);
  },
  methods: {
    initFn() {
      this.isSingle = true;
      this.singleIndex = 0;
    },
    changeHeight(isSingleMulti) {
      if (isSingleMulti) {
        this.isSingle = !this.isSingle;
      }
      this.isChangeH = !this.isChangeH;
      this.$emit('changeHeight', this.isChangeH);
    },
    //获取某个月份的天数
    getCountDays(date) {
      let copy_date = cloneDeep(date);
      /*  生成实际的月份: 由于curMonth会比实际月份小1, 故需加1 */
      copy_date.setMonth(copy_date.getMonth() + 1);
      copy_date.setDate(0);
      /* 返回当月的天数 */
      return copy_date.getDate();
    },
    getMomentFormat(elFormat) {
      let format = elFormat.replace(/y/g, 'Y').replace(/d/g, 'D');
      return format;
    },
    confirmSearch(aparams) {
      // 单项搜索只能以一个条件搜索 条件不叠加start
      let selectArr = this.selectionList.slice(0);
      let selectArrSingle = [];
      selectArrSingle.push(selectArr[this.singleIndex]);
      // 单项搜索只能以一个条件搜索 条件不叠加end
      let selectArrSearch = selectArr;
      let params = $.extend({}, aparams);
      for (let i = 0, len = selectArrSearch.length; i < len; i++) {
        let thisObj = selectArrSearch[i];
        let Val = thisObj.value;
        let name = thisObj.name;
        if (Val) {
          if (thisObj.isSingle) {
            if (thisObj.format) {
              Val = moment(Val).format(this.getMomentFormat(thisObj.format));
            } else {
              if ('date' == thisObj.flag) {
                Val = moment(Val).format('YYYY-MM-DD');
              } else if ('month' == thisObj.flag) {
                Val = moment(Val).format('YYYY-MM');
              }
            }
            params[name] = Val;
          } else if ('datePicker' == thisObj.type || 'timePicker' == thisObj.type) {
            let propArr = thisObj.propName.split(',');
            if (Val && Val.constructor == Array && Val.length > 0) {
              if ('timePicker' == thisObj.type && Val[0]) {
                let _dateVal0 = new Date(Val[0]);
                let _dateVal1 = new Date(Val[1]);
                let _hour0 = _dateVal0.getHours();
                let _minute0 = formatTen(_dateVal0.getMinutes());
                let _second0 = formatTen(_dateVal0.getSeconds());
                let _hour1 = _dateVal1.getHours();
                let _minute1 = formatTen(_dateVal1.getMinutes());
                let _second1 = formatTen(_dateVal1.getSeconds());
                let year = moment().year();
                let month = formatTen(moment().month());
                let day = formatTen(moment().date());
                let dateVal0 = new Date(year, month, day, _hour0, _minute0, _second0);
                let dateVal1 = new Date(year, month, day, _hour1, _minute1, _second1);
                Val[0] = dateVal0;
                Val[1] = dateVal1;
              }
              if (Val[0]) {
                params[propArr[0]] = formatDate(Val[0]);
              } else {
                params[propArr[0]] = undefined;
              }
              if (Val[1]) {
                if ('daterange' == thisObj.pickerType) {
                  let _Val1 = Val[1];
                  _Val1 =
                    _Val1.getFullYear() +
                    '-' +
                    formatTen(_Val1.getMonth() + 1) +
                    '-' +
                    formatTen(_Val1.getDate()) +
                    ' 23:59:59';
                  params[propArr[1]] = _Val1;
                } else {
                  params[propArr[1]] = formatDate(Val[1]);
                }
              } else {
                params[propArr[1]] = undefined;
              }
            } else {
              if (Val) {
                let year = Val.getFullYear();
                let month = formatTen(Val.getMonth() + 1);
                let _start = '';
                let _end = '';
                if ('year' == thisObj.pickerType) {
                  _start = year + '-01-01 00:00:00';
                  _end = year + '-12-31 23:59:59';
                } else if ('month' == thisObj.pickerType) {
                  let days = this.getCountDays(Val);
                  _start = year + '-' + month + '-01 00:00:00';
                  _end = year + '-' + month + '-' + days + ' 23:59:59';
                }
                params[propArr[0]] = _start;
                params[propArr[1]] = _end;
              }
            }
          } else if ('doubleInput' == thisObj.type) {
            let names = thisObj.names;
            let values = thisObj.values;
            if (values[0] !== '') {
              params[names[0]] = values[0];
            }
            if (values[1] !== '') {
              params[names[1]] = values[1];
            }
          } else {
            params[name] = Val;
          }
        }
      }
      this.$emit('confirmSearch', params);
    }
  }
};
